﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Data;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public class Common
    {
        /// <summary>
        /// 栏目导航
        /// </summary>
        public static String ColumnOperate(string Permissions, string Operate, string Url, bool ShowAll, string typeID)
        {
            CNVP.JShop.BLL.CNVP_Column bll = new CNVP.JShop.BLL.CNVP_Column();
            string ParentID = "0";
            if (!String.IsNullOrEmpty(HttpContext.Current.Request.Params["TypeID"]) && Public.IsNumber(HttpContext.Current.Request.Params["TypeID"]))
            {
                typeID = HttpContext.Current.Request.Params["TypeID"];
            }

            if (!String.IsNullOrEmpty(HttpContext.Current.Request.Params["ParentID"]) && Public.IsNumber(HttpContext.Current.Request.Params["ParentID"]))
            {
                ParentID = HttpContext.Current.Request.Params["ParentID"];
            }
            DataTable dt = null;
            String r = String.Empty;
            if (ParentID == "0")
            {
                if (typeID == "0")
                {
                    dt = bll.ColumnOperate(ShowAll, Convert.ToInt32(ParentID));
                    r = Common.DtToJSON(Operate, Url, dt, typeID);
                }
                else
                {
                    dt = bll.ColumnOperate(Convert.ToInt32(typeID), ShowAll, Convert.ToInt32(ParentID));
                    r = Common.DtToJSON(Operate, Url, dt, Convert.ToInt32(typeID));
                }
            }
            else
            {
                if (typeID == "0")
                {
                    dt = bll.ColumnOperate(false, Convert.ToInt32(ParentID));
                    r = Common.DtToJSON(Operate, Url, dt, typeID);
                }
                else
                {
                    dt = bll.ColumnOperate(Convert.ToInt32(typeID), false, Convert.ToInt32(ParentID));
                    r = Common.DtToJSON(Operate, Url, dt, Convert.ToInt32(typeID));
                }
            }
            return r;
        }
        /// <summary>
        /// 获取JSON数据
        /// </summary>
        /// <param name="Dt"></param>
        /// <returns></returns>
        public static String DtToJSON(String Operate, String Url, DataTable Dt, string typeID)
        {
            StringBuilder Str = new StringBuilder();
            Str.Append("{");
            Str.Append("\"nodes\":[");
            for (int i = 0; i < Dt.Rows.Count; i++)
            {
                switch (Operate)
                {
                    //模板导航
                    case "TemplateSetLeft":
                        Str.Append("{");
                        if (new BLL.CNVP_Column().IsChildMenu(Convert.ToInt32(Dt.Rows[i]["ID"])))
                        {
                            Str.Append("\"source\":\"TemplateSetAjax.aspx?Action=TemplateSetLeft&ParentID=" + Dt.Rows[i]["ID"].ToString() + "&typeID=" + typeID + "\",");
                        }
                        Str.Append("\"text\":\"" + Dt.Rows[i]["ColumnName"].ToString() + "\"");
                        Str.Append(",\"url\":\"" + Url + "?ParentID=" + Dt.Rows[i]["ID"].ToString() + "&typeID=" + typeID + "\"");
                        Str.Append("},");
                        break;
                    //栏目导航
                    case "ColumnLeft":
                        Str.Append("{");
                        if (new BLL.CNVP_Column().IsChildMenu(Convert.ToInt32(Dt.Rows[i]["ID"])))
                        {
                            Str.Append("\"source\":\"../JShopAjax.aspx?Action=ColumnLeft&ParentID=" + Dt.Rows[i]["ID"].ToString() + "&typeID=" + typeID + "\",");
                        }
                        Str.Append("\"text\":\"" + Dt.Rows[i]["ColumnName"].ToString() + "\"");
                        Str.Append(",\"url\":\"" + Url + "?ParentID=" + Dt.Rows[i]["ID"].ToString() + "\"");
                        Str.Append("},");
                        break;
                    //栏目转移
                    case "ColumnMove":
                        Str.Append("{");
                        string _ColumnID = HttpContext.Current.Request.Params["ColumnID"];
                        if (new BLL.CNVP_Column().IsChildMenu(Convert.ToInt32(Dt.Rows[i]["ID"])))
                        {
                            Str.Append("\"source\":\"../JShopAjax.aspx?Action=ColumnMove&ParentID=" + Dt.Rows[i]["ID"].ToString() + "&ColumnID=" + _ColumnID + "&typeID=" + typeID + "\",");
                        }
                        Str.Append("\"text\":\"" + Dt.Rows[i]["ColumnName"].ToString() + "\"");
                        Str.Append(",\"url\":\"" + Url + "?ParentID=" + Dt.Rows[i]["ID"].ToString() + "&ColumnID=" + _ColumnID + "\"");
                        Str.Append("},");
                        break;
                    //模板设置
                    case "TemplateLeft":
                        Str.Append("{");
                        if (new BLL.CNVP_Column().IsChildMenu(Convert.ToInt32(Dt.Rows[i]["ID"])))
                        {
                            Str.Append("\"source\":\"../JShopAjax.aspx?Action=TemplateLeft&ParentID=" + Dt.Rows[i]["ID"].ToString() + "&typeID=" + typeID + "\",");
                        }
                        Str.Append("\"text\":\"" + Dt.Rows[i]["ColumnName"].ToString() + "\"");
                        Str.Append(",\"url\":\"" + Url + "?ParentID=" + Dt.Rows[i]["ID"].ToString() + "\"");
                        Str.Append("},");
                        break;
                    //设置栏目
                    case "ColumnSet":
                        Str.Append("{");
                        if (new BLL.CNVP_Column().IsChildMenu(Convert.ToInt32(Dt.Rows[i]["ID"])))
                        {
                            Str.Append("\"source\":\"JShopAjax.aspx?Action=ColumnSet&ParentID=" + Dt.Rows[i]["ID"].ToString() + "&typeID=" + typeID + "\",");
                        }
                        Str.Append("\"text\":\"" + Dt.Rows[i]["ColumnName"].ToString() + "\"");
                        Str.Append(",\"url\":\"" + Url + "?ParentID=" + Dt.Rows[i]["ID"].ToString() + "\"");
                        Str.Append("},");
                        break;
                }
            }
            Str.Append("]");
            Str.Append("}");
            return Str.ToString();
        }
        public static string DtToJSON(string Operate, string Url, DataTable Dt, int typeID)
        {
            StringBuilder Str = new StringBuilder();
            Str.Append("{");
            Str.Append("\"nodes\":[");
            for (int i = 0; i < Dt.Rows.Count; i++)
            {
                switch (Operate)
                {
                    //模板导航
                    case "TemplateSetLeft":
                        Str.Append("{");
                        if (new BLL.CNVP_Column().IsChildMenu(Convert.ToInt32(Dt.Rows[i]["ID"])))
                        {
                            Str.Append("\"source\":\"TemplateSetAjax.aspx?Action=TemplateSetLeft&ParentID=" + Dt.Rows[i]["ID"].ToString() + "&typeID=" + typeID + "\",");
                        }
                        Str.Append("\"text\":\"" + Dt.Rows[i]["ColumnName"].ToString() + "\"");
                        Str.Append(",\"url\":\"" + Url + "?ParentID=" + Dt.Rows[i]["ID"].ToString() + "&typeID=" + typeID + "\"");
                        Str.Append("},");
                        break;
                    //栏目导航
                    case "ColumnLeft":
                        Str.Append("{");
                        if (new BLL.CNVP_Column().IsChildMenu(Convert.ToInt32(Dt.Rows[i]["ID"]), typeID))
                        {
                            Str.Append("\"source\":\"../JShopAjax.aspx?Action=ColumnLeft&ParentID=" + Dt.Rows[i]["ID"].ToString() + "&typeID=" + typeID + "\",");
                        }
                        Str.Append("\"text\":\"" + Dt.Rows[i]["ColumnName"].ToString() + "\"");
                        Str.Append(",\"url\":\"" + Url + "?ParentID=" + Dt.Rows[i]["ID"].ToString() + "\"");
                        Str.Append("},");
                        break;
                    //栏目转移
                    case "ColumnMove":
                        Str.Append("{");
                        string _ColumnID = HttpContext.Current.Request.Params["ColumnID"];
                        if (new BLL.CNVP_Column().IsChildMenu(Convert.ToInt32(Dt.Rows[i]["ID"]), typeID))
                        {
                            Str.Append("\"source\":\"../JShopAjax.aspx?Action=ColumnMove&ParentID=" + Dt.Rows[i]["ID"].ToString() + "&ColumnID=" + _ColumnID + "&typeID=" + typeID + "\",");
                        }
                        Str.Append("\"text\":\"" + Dt.Rows[i]["ColumnName"].ToString() + "\"");
                        Str.Append(",\"url\":\"" + Url + "?ParentID=" + Dt.Rows[i]["ID"].ToString() + "&ColumnID=" + _ColumnID + "\"");
                        Str.Append("},");
                        break;
                    //模板设置
                    case "TemplateLeft":
                        Str.Append("{");
                        if (new BLL.CNVP_Column().IsChildMenu(Convert.ToInt32(Dt.Rows[i]["ID"]), typeID))
                        {
                            Str.Append("\"source\":\"../JShopAjax.aspx?Action=TemplateLeft&ParentID=" + Dt.Rows[i]["ID"].ToString() + "&typeID=" + typeID + "\",");
                        }
                        Str.Append("\"text\":\"" + Dt.Rows[i]["ColumnName"].ToString() + "\"");
                        Str.Append(",\"url\":\"" + Url + "?ParentID=" + Dt.Rows[i]["ID"].ToString() + "\"");
                        Str.Append("},");
                        break;
                    //设置栏目
                    case "ColumnSet":
                        Str.Append("{");
                        if (new BLL.CNVP_Column().IsChildMenu(Convert.ToInt32(Dt.Rows[i]["ID"]), typeID))
                        {
                            Str.Append("\"source\":\"JShopAjax.aspx?Action=ColumnSet&ParentID=" + Dt.Rows[i]["ID"].ToString() + "&typeID=" + typeID + "\",");
                        }
                        Str.Append("\"text\":\"" + Dt.Rows[i]["ColumnName"].ToString() + "\"");
                        Str.Append(",\"url\":\"" + Url + "?ParentID=" + Dt.Rows[i]["ID"].ToString() + "\"");
                        Str.Append("},");
                        break;
                }
            }
            Str.Append("]");
            Str.Append("}");
            return Str.ToString();
        }
        public static string DtToJSON1(string Operate, string Url, DataTable Dt, int typeID)
        {
            StringBuilder Str = new StringBuilder();
            Str.Append("{");
            Str.Append("\"nodes\":[");
            for (int i = 0; i < Dt.Rows.Count; i++)
            {
                Str.Append("{");
                //判断是否存在下级节点
                if (new BLL.CNVP_Column().IsChildMenu(Convert.ToInt32(Dt.Rows[i]["ID"]), typeID))
                {
                    Str.Append("\"source\":\"JShopAjax.aspx?Action=" + Operate + "&ParentID=" + Dt.Rows[i]["ID"].ToString() + "&typeID=" + typeID + "\",");
                }
                Str.Append("\"text\":\"" + Dt.Rows[i]["ColumnName"].ToString() + "\"");
                Str.Append(",\"url\":\"" + Url + "?ParentID=" + Dt.Rows[i]["ID"].ToString() + "\"");
                Str.Append("},");
            }
            Str.Append("]");
            Str.Append("}");
            return Str.ToString();
        }
    }
}